﻿using System;
using System.Data.SqlClient;
using System.Data;
using Obj;
using System.Collections.Generic;

namespace DAL
{

    /// <summary>
    /// Summary description for SQLPermissao
    /// </summary>
    public class SQLPermissao : dataAccess
    {
        public SQLPermissao() { }

        //public bool Delete(int permissaoID)
        //{
        //    using (SqlConnection con = new SqlConnection(this.ConnectionString))
        //    {
        //        SqlCommand cmd = new SqlCommand("permissao_Delete", con);
        //        cmd.CommandType = CommandType.StoredProcedure;
        //        cmd.Parameters.Add("@permissaoID", SqlDbType.Int).Value = permissaoID;
        //        con.Open();
        //        int ret = ExecuteNonQuery(cmd);
        //        return (ret == 1);
        //    }
        //}

        public bool Update(objPermissao permissao)
        {
            using (SqlConnection con = new SqlConnection(this.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("permissao_Update", con);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@permissaoID", SqlDbType.Int).Value = permissao.PermissaoID;
                cmd.Parameters.Add("@descricao", SqlDbType.NVarChar).Value = permissao.Descricao;
                con.Open();
                int ret = ExecuteNonQuery(cmd);
                return (ret == 1);

            }
        }

        public int Insert(objPermissao permissao)
        {
            using (SqlConnection con = new SqlConnection(this.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("permissao_Insert", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@descricao", SqlDbType.NVarChar).Value = permissao.Descricao;
                cmd.Parameters.Add("@permissaoID", SqlDbType.Int).Direction = ParameterDirection.Output;
                con.Open();
                int ret = ExecuteNonQuery(cmd);
                return (int)cmd.Parameters["@permissaoID"].Value;
            }
        }

        public objPermissao SelectByID(int permissaoID)
        {
            using (SqlConnection con = new SqlConnection(this.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("permissao_SelectByID", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@permissaoID", SqlDbType.Int).Value = permissaoID;
                con.Open();
                IDataReader reader = ExecuteReader(cmd);
                if (reader.Read())
                    return obj(reader);
                else
                    return null;
            }
        }

        public List<objPermissao> SelectAll()
        {
            using (SqlConnection con = new SqlConnection(this.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("permissao_selectAll", con);
                cmd.CommandType = CommandType.StoredProcedure;
                con.Open();
                return objLista(ExecuteReader(cmd));
            }
        }
        
        public bool Delete(int permissaoID)
        {
            using (SqlConnection con = new SqlConnection(this.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("permissao_Delete", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@permissaoID", SqlDbType.Int).Value = permissaoID;
                con.Open();
                int ret = ExecuteNonQuery(cmd);
                return (ret == 1);
            }
        }

        public List<objPermissao> SelectAll(objPermissao permissao)
        {
            using (SqlConnection con = new SqlConnection(this.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("permissao_selectAll", con);
                cmd.Parameters.Add("@descricao", SqlDbType.NVarChar).Value = permissao.Descricao;
                cmd.CommandType = CommandType.StoredProcedure;
                con.Open();
                return objLista(ExecuteReader(cmd));
            }
        }
        //Na Primeira versão não é necessário implementar  o metodo select By Id no objeto permissao

        private objPermissao obj(IDataReader reader)
        {
            objPermissao permissao = new objPermissao(
                                                    (int)reader["permissaoID"],
                                                    reader["descricao"].ToString()
                                                );
            return permissao;
        }

        private List<objPermissao> objLista(IDataReader reader)
        {
            List<objPermissao> permissoes = new List<objPermissao>();
            while (reader.Read())
                permissoes.Add(obj(reader));
            return permissoes;

        }




    }
}